package com.xielx.httpclikent.bean;

import com.alibaba.fastjson.JSONObject;

import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;

public class ProxyRequest extends MyHttpPost implements Serializable {
    private static final long serialVersionUID = -4056019860056695331L;
    private MyHttpRequest request;

    public ProxyRequest(String url) {
        super(url);
    }

    public ProxyRequest(String url, MyHttpRequest request) {
        super(url);
        this.request = request;
        addHeader("Content-Type","application/json");
    }

    @Override
    public byte[] getData() {
        JSONObject data = new JSONObject();
        data.put("header", this.request.getHeader());
        data.put("param", this.request.getParams());
        data.put("method", this.request.getMethod());
        data.put("url", this.request.getUrl());
        if (request.isPost()) {
            data.put("data", ((MyHttpPost) this.request).getData());
        }
        return data.toJSONString().getBytes();
    }

    @Override
    public URL toURL() throws UnsupportedEncodingException, MalformedURLException {
        return new URL(getUrl());
    }
}
